package com.mc.lp.database.service.handler;

import java.util.HashSet;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;

import com.mc.lp.database.exception.ConcurrentAccessException;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.service.ExtraAcademicService;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.SimpleResponse;
import com.mc.lp.shared.vo.StudentRecordVO;

public class ExtraAcademicRecordHandler extends AbstractFormHandler<IValueObject, IValueObject>{
	/**
	 * 
	 */
	@Resource
	ExtraAcademicService extraAcademicService;
	
	@Override
	public HashSet<FormRequestType> getRequestHandlingType() {
		HashSet<FormRequestType> requestTypeHandled= new HashSet<FormRequestType>();
		requestTypeHandled.add(FormRequestType.SAVE_EXTRA_ACADEMICS);
		requestTypeHandled.add(FormRequestType.GET_EXTRA_ACADEMICS_BY_GRADE);
		return requestTypeHandled;
	}

	@Override
	public IValueObject serveRequest(FormRequestType requestType, IValueObject object) throws Exception {
		//simple response to be returned to UI calls if no match found, otherwise return serviceResponseclass
		SimpleResponse response = new SimpleResponse();
		response.setId(1);
		response.setMsg("No case matched to perform operation, please check request type");
		
		//convert request to student record vo object
		StudentRecordVO studentVo=null;
		if(object !=null)
			studentVo=(StudentRecordVO)object;
		
		switch (requestType) {
		case GET_EXTRA_ACADEMICS_BY_GRADE:
			IValueObject result= getExtraAcademicByGrade(studentVo);
			if(result==null){
				response.setMsg("Not able to get data from server");
				return response;
			}
			return result;
		case SAVE_EXTRA_ACADEMICS:
			IValueObject saveResult= saveExtraAcademic(studentVo);
			if(saveResult == null){
				response.setMsg("Not able to save data from server");
				return response;
			}
			return saveResult;
		default:
			return response;
		}
	}
	/**
	 * get extra academic record by grade id
	 * @param object
	 * @return
	 */
	private IValueObject getExtraAcademicByGrade(StudentRecordVO object){
		IValueObject result=null;
		if(object !=null){
			result=getExtraAcademicService().getExtraAcademicsRecordsByEntityIdGrade(object);
		}
		return result;
	}
	
	/**
	 * save extra academics
	 * @param object
	 * @return
	 * @throws ConcurrentAccessException 
	 * @throws DataAccessException 
	 */
	private IValueObject saveExtraAcademic(StudentRecordVO object) throws DataAccessException, ConcurrentAccessException{
		IValueObject result=null;
		if(object !=null){
			result=getExtraAcademicService().saveExtraAcademics(object);
		}
		return result;
	}

	public ExtraAcademicService getExtraAcademicService() {
		return extraAcademicService;
	}

	public void setExtraAcademicService(ExtraAcademicService extraAcademicService) {
		this.extraAcademicService = extraAcademicService;
	}
	
	
}
